<template>
  <div style="height: 100%;">
    <x-header :left-options="{backText: ''}">每日考勤</x-header>
    <div class="main-container">
      <cell title="项目名称" value-align="right" class="cell">
        <div class="black">{{projInfo.PROJECTNAME || sysVar.PRJNAME}}</div>
      </cell>
      <cell title="项目编号" value-align="right">
        <div class="black">{{projInfo.PROJECTNO || sysVar.PRJNO}}</div>
      </cell>
      <group>
        <datetime title="日志时间" v-model="noteDate" placeholder='请选择日志时间'></datetime>
      </group>
      <group label-width="5em">
        <popup-picker title="考勤" v-model="amatt" :data='workTpye' placeholder='请选择'></popup-picker>
      </group>
      <group title="项目日志明细">
        <x-textarea v-model="detail" placeholder='请输入项目日志明细' show-counter></x-textarea>
      </group>
      <group title="技术质量安全工作">
        <x-textarea v-model="safeJob" placeholder='请输入技术质量安全工作' show-counter></x-textarea>
      </group>

      <div style="padding: 10px 15px">上传图片</div>
      <picture-upload></picture-upload>
      <cell title="填写人" value-align="right" class="cell">
        <div class="black">{{userInfo.userName}}</div>
      </cell>
      <cell title="填写时间" value-align="right" class="cell">
        <div class="black">{{inputDate}}</div>
      </cell>
    </div>
    <x-button class="hidanger-button" @click.native="confirm" type='primary'>保存</x-button>
    <router-view></router-view>
  </div>
</template>

<script>
import { log } from 'util';
import moment from 'moment'
export default {
  name: "projectList",
  components: {},
  created () {
    this.getWorkType()
    this.getDate()
    this.getContent()
  },
  mounted () {
  },
  data () {
    return {
      createPop: false,//弹出层是否创建
      loading: false,//下拉刷新时禁止无限加载
      noteDate: '',
      params: {},
      workTpye: [],
      amatt: ['出勤'],
      detail: '',
      safeJob: '',
      inputDate: '',
      GUID: '', 
      buttonId: '',
      sysVar: {},
    }
  },
  methods: {
    getWorkType() {
      let url = '?_viewId=PMCOM_DAYLOGS&_menuId=f60d79a4-feaf-4967-9c85-be231bb28465&objName=PMCOM_DAYLOGS&_treeNodeId=&_treeNodeName=undefined&_isBreak=false&_useType=add'
      let cookies = ''
      let FixJSON = {
        _method: "component.getBatchDatas",
        _param: {
          controls: {
            AMATT: {
              orgConfig: {
                Filter: "TYPE_ID='PMHUM_ATTENDANCE.ATTTYPE'",
                Fields: "CODE_ID,CODE_NAME",
                BizObj: "FIX_SYS_CODE",
                Service: "selectMore",
                componentType: "ECIDIHTMLSelect"
              },
              dataConfig: {
                Method: "component.getListData",
                Param: {
                  bizObj: "FIX_SYS_CODE",
                  service: "selectMore",
                  fields: "CODE_ID,CODE_NAME",
                  filter: "TYPE_ID='PMHUM_ATTENDANCE.ATTTYPE'",
                  currentPageIndex: 1,
                  pageSize: 999999,
                  orderList: [],
                  QueryFormData: ""
                },
                componentType: "ECIDIHTMLSelect"
              },
              storeType: "Data"
            }
          }
        }
      }
      FixJSON = JSON.stringify(FixJSON)
      // debugger
      this.$ajax({
        url: this.COMMON_URL, 
        method: 'get',
        params: {FixJSON,cookies}
      }).then(res => {
        this.setType(res)
        console.log(res)
      })
    },
    setType(res) {
      let list = []
      res.data.datas.AMATT.map((item, i) => {
        list.push(item.CODE_NAME)
      })
      this.workTpye.push(list)
      console.log(this.workTpye)
    },
    getDate() {
      let nowDate = new Date();
      this.noteDate = moment().format('YYYY-MM-DD')
      this.inputDate = moment().format('YYYY-MM-DD')
      console.log(this.noteDate)
    },
    getContent() {
      let params = '{"_method":"formService.getFormData","requestEventData":{"eventType":"formInitEvent","formType":1,"bizObjId":"PMCOM_DAYLOGS","formId":"PMCOM_DAYLOGS_ADD"},"useType":"add","getSysVar":{"GUID":"{sysIndex.GUID}","SYSPRJID":"{sysLoginUser.projectId}","PRJNAME":"{sysLoginUser.projectName}","PRJNO":"{sysLoginUser.projectNo}","PROJECTTIME":"{sysDate.Today}","time1":"{sysDate.Today}","INPUTERFULLNAME":"{sysLoginUser.userName}","INPUTERNAME":"{sysLoginUser.loginId}","INPUTERDEPTNAME":"{sysLoginUser.orgName}","INPUTERDEPTID":"{sysLoginUser.prjDeptId}","INPUTDATE":"{sysDate.Today}"},"getFormVerify":{"objName":"PMCOM_DAYLOGS","viewId":"PMCOM_DAYLOGS_ADD"},"getToolbar":{"useType":"add","objName":"PMCOM_DAYLOGS","viewId":"PMCOM_DAYLOGS_ADD"}}'
      
      //获取业务数据（主表、明细表（如果有））及任务处理的按钮信息
      this.$ajax({
        method: 'post',
        url: this.COMMON_URL+'?_viewId=PMCOM_DAYLOGS&_menuId=f60d79a4-feaf-4967-9c85-be231bb28465&objName=PMCOM_DAYLOGS&_treeNodeId=&_treeNodeName=undefined&_isBreak=false&_useType=add',
        data : {
          FixJSON: params
        }
      }).then((res) => {
        console.log(res)
        //业务数据
        this.sysVar = res.data.getSysVar
        this.buttonId = res.data.getToolbar[0][0].id || '';
        this.GUID = res.data.getSysVar.GUID || ''
      }).catch((err)=>{
        console.log(err);
      });
    },
    sPageToken(){
      return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
            let r = Math.random() * 16 | 0,
            v = c == 'x' ? r : (r & 0x3 | 0x8);
            return v.toString(16);
        });
    },
    confirm(){
      let obj = {};
      let rowData = [];
      rowData[0] = this.setRowData("GUID", this.GUID, null, true, "String");
      rowData[1] = this.setRowData("SYSPRJID", this.sysVar.SYSPRJID, null, false, "String");
      rowData[2] = this.setRowData("PRJNAME",  this.sysVar.PRJNAME, null, false, "String");
      rowData[3] = this.setRowData("PRJNO", this.sysVar.PRJNO, null, false, "String");
      rowData[4] = this.setRowData("PROJECTTIME", this.noteDate, null, false, "TimeStamp");
      rowData[5] = this.setRowData("AMATT", this.amatt[0], null, false, "String");
      rowData[6] = this.setRowData("PROJECTSTATUS", this.detail, null, false, "String");
      rowData[6] = this.setRowData("SAFEJOB", this.safeJob, null, false, "String");
      rowData[7] = this.setRowData("ATTACHMENT", "094f9d6b-d0db-9e2c-944c-89d32419636b", null, false, "Store.ecidiDoc");
      rowData[8] = this.setRowData("INPUTERFULLNAME", this.sysVar.INPUTERFULLNAME, null, false, "String");
      rowData[9] = this.setRowData("INPUTERNAME", this.sysVar.INPUTERNAME, null, false, "String");
      rowData[10] = this.setRowData("INPUTERDEPTNAME", this.sysVar.INPUTERDEPTNAME, null, false, "String");
      rowData[11] = this.setRowData("INPUTERDEPTID", this.sysVar.INPUTERDEPTID, null, false, "Store.ecidiDoc");
      rowData[12] = this.setRowData("INPUTDATE", this.sysVar.INPUTDATE, null, false, "TimeStamp");
      let formInfo = {}
      rowData.map(item => {
        formInfo[item.DataTarget] = item.Value
      })

      obj = {
        _method: "formService.saveFormData",
        requestEventData: {
          eventType: "formSubmitEvent",
          formType: 1,
          bizObjId: "PMCOM_DAYLOGS",
          formId: "PMCOM_DAYLOGS_ADD",
          buttonId: this.buttonId
        },
        _OBJ_NAME_: "PMCOM_DAYLOGS",
        _FORM_ID_: "PMCOM_DAYLOGS_ADD",
        useType: "add",
        pageToken: "e81260ae-a0f1-511e-1356-ddd3a2458a28",
        formData: {
          objName: "PMCOM_DAYLOGS",
          pks: ["GUID"],
          data: [{
            rowData: rowData,
            children: []
          }],
        },
        _ATTACHMENT_DELETE_IDS_: '',
        formInfo: formInfo
      }
      console.log(obj)
      if(this.detail == ''){
        this.$vux.toast.text('项目日志明细不能为空', 'middle');
        return false
      }
      obj = JSON.stringify(obj)
      this.$vux.loading.show({
        text: '正在提交'
      })
      this.$ajax({
        method: 'post',
        url: this.COMMON_URL +'?_viewId=PMCOM_DAYLOGS&_menuId=f60d79a4-feaf-4967-9c85-be231bb28465&objName=PMCOM_DAYLOGS&_treeNodeId=&_treeNodeName=undefined&_isBreak=false&_useType=add',
        data: {
          FixJSON: obj
        }
      }).then((res) => {
        console.log(res)
        this.$vux.loading.hide()
        if(res.data.attenancelist){
          if(res.data.attenancelist[0].ADDRESS == "电脑端已签到")
          this.$vux.toast.text(res.data.attenancelist[0].ADDRESS);
          setTimeout(() => {
            this.$router.go(-1)
          }, 1000);
        }else{
          this.$vux.toast.text('保存失败');
        }
      }).catch((err)=>{
        console.log(err);
        alert(err);
      });
    },
    setRowData(DataTarget, Value, OriginalValue, PK, DataType){
      var map = {};
      map["DataTarget"] = DataTarget;
      map["Value"] = Value;
      map["OriginalValue"] = OriginalValue;
      map["PK"] = PK;
      map["DataType"] = DataType;
      return map;
    },
      
  },
  computed: {
    projInfo () {
      return this.$store.state.projInfo
    },
    userInfo () {
      return this.$store.state.user
    }
    // mainPop () {
    //   return this.$store.state.mainPop
    // }
  },
  watch: {
    '$route' (newR, oldR) {
      if(this.$store.state.needRefresh){
        this.refresh()
        this.$store.commit("needRefreshStatus",false)
      }
    },

  }
}
</script>

<style scoped>
  .cell >>> .vux-label{
    width: 80px;
  }
  .main-container{
    overflow-x: hidden
  }
</style>
